package com.liuzhilong.infrastructure.basic.proxy;

import com.liuzhilong.infrastructure.basic.proxy.dynamic.cglib.SimpleCGLIBProxy;
import com.liuzhilong.infrastructure.basic.proxy.dynamic.jdk.SimpleJDKProxy;
import com.liuzhilong.infrastructure.basic.proxy.fixed.Proxy;

/**
 * 测试
 * @author longandai@163.com
 */
public class UseCase {
    public static void main(String[] args) {
//        useFixedProxy();
//        useJdkProxy();
        userCglibProxy();
//        userCglibProxyFinal();
    }
    private static final void useFixedProxy(){
        //原始的
        Subject subject = new RealSubject();
        subject.do1();
        System.out.println("---------------------");
        //使用代理的
        Subject subject1 = new Proxy();
        subject1.do1();

    }

    /**
     * <p>
     *     测试jdk的代理。
     * </p>
     * 我们可以看到jdk的代理会拦截目标的所有方法。<br>
     *  如果需要做特殊控制的话，需要使用method进行拦截处理
     */
    private static final void useJdkProxy(){
        RealSubject target = new RealSubject();
        Subject subject = SimpleJDKProxy.createProxy(target);
//        subject.do1();
        System.out.println(subject.do2());
//        System.out.println(subject.do3(" test"));
//        System.out.println(subject.toString());
    }

    /**
     * <p>测试cglib的大力</p>
     */
    private static final void userCglibProxy(){
        RealSubject proxiedInfo = SimpleCGLIBProxy.create(RealSubject.class);
//        proxiedInfo.do1();
//        System.out.println(proxiedInfo.do2());
        System.out.println(proxiedInfo.do3(" test"));
//        System.out.println(proxiedInfo.toString());

    }
    /**
     * <p>测试cglib的大力</p>
     */
    private static final void userCglibProxyFinal(){
        FinalSubject proxiedInfo = SimpleCGLIBProxy.create(FinalSubject.class);
        proxiedInfo.do1();
        System.out.println(proxiedInfo.do2());
        System.out.println(proxiedInfo.do3(" test"));
        System.out.println(proxiedInfo.toString());

    }

}
